package com.seeyon.ctp.rest.resources;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.brwh.kit.StrKit;
import com.seeyon.apps.brwh.manager.EarlyWarningManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.commons.logging.Log;


import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预警图对外暴露接口 getEarlyWarningList：获取预警图列表数据
 * @author ys
 * @create 2024-07-05-15:12
 */
@Path("jssdk/SRMKB")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class EarlyWarningResources extends BaseResource {
    private static final Log log = CtpLogFactory.getLog(EarlyWarningResources.class);

    private EarlyWarningManager earlyWarningManager = (EarlyWarningManager) AppContext.getBean("earlyWarningManager");
    @GET
    @Path("getEarlyWarningList")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({ MediaType.APPLICATION_JSON })
    public Response getEarlyWarningList(Map<String,Object> parm) throws Exception {
        Map<String, Object> result=earlyWarningManager.getEarlyWarningResources();
        return this.ok(JSONObject.toJSONString(result));
    }

    @POST
    @Path("procurement/info/datas")
    public Response ipdProjectInfoData(Map<String, Object> param) throws Exception {
        // 记录请求参数的日志信息。
        log.info("采购可视化模块前端入参:" + JSON.toJSONString(param));
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        try {
            // 从param Map中提取项目名称、开始时间和结束时间。
            JSONObject jsonObject = new JSONObject(param);
            /*波次*/
            String waves = "";
            if (jsonObject.containsKey("waves")) {
                waves = StrKit.str(jsonObject.get("waves"));
            }
            /*项目名称*/
            String itemName = "";
            if (jsonObject.containsKey("itemName")) {
                itemName = StrKit.str(jsonObject.get("itemName"));
            }
            /*项目编码*/
            String itemCode = "";
            if (jsonObject.containsKey("itemCode")) {
                itemCode = StrKit.str(jsonObject.get("itemCode"));
            }
            /*项目经理姓名*/
            String projectManagerName = "";
            if (jsonObject.containsKey("projectManagerName")) {
                projectManagerName = StrKit.str(jsonObject.get("projectManagerName"));
            }
            /*计划结束时间*/
            String planEndTime= "";
            if (jsonObject.containsKey("planEndTime")) {
                planEndTime = StrKit.str(jsonObject.get("planEndTime"));
            }

            // 调用earlyWarningManager的查询方法，获取项目信息数据列表
            dataList = earlyWarningManager.getProjectInfoDataList(waves, itemName, itemCode, projectManagerName, planEndTime);

            // 设置返回结果的代码、数据和消息字段。
            result.put("code", 200);
            result.put("data", dataList);
            result.put("msg", "");
            return ok(JSON.toJSONString(result));
        } catch (Exception e) {
            // 记录业务异常的日志信息。
            log.error("获取项目信息模块数据报错异常信息:", e);
            // 设置返回结果的错误代码、空数据和错误消息。
            result.put("code", 500);
            result.put("data", "");
            result.put("msg", e.getMessage());
            return ok(JSON.toJSONString(result));
        }
    }
}
